package com.ftg.learn.chapter09.collection;

import sun.util.resources.ga.LocaleNames_ga;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * list的常 用lambda表达式
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/14 10:04
 */

public class ListLambda {

    public static void main(String[] args) {
        List<String> list1= Arrays.asList("张三","张三","王五","张三丰");
        List<String> list2= Arrays.asList("张三1","张三1","王五1","张三丰1");
        List<Object> list = new ArrayList<>();

        for (int i = 0; i < list1.size(); i++) {
                System.out.printf("下标为%d,内容为%s",i,list1.get(i));
            }

            for (String s : list1) {
                System.out.println("内容为\n"+s);
            }

            System.out.println("迭代器");
            Iterator<String> it = list1.iterator();
            for(;it.hasNext();){
                String obj=it.next();
                System.out.println(obj);
            }

        for (Iterator<String> it1 = list1.iterator();it.hasNext(); ) {
            String obj=it.next();
            System.out.println(obj);
        }

        Iterator<String> it2 = list1.iterator();
        while (it2.hasNext()){
            String obj=it2.next();
            System.out.println(obj);
        }
            System.out.println("迭代器");

        System.out.println("lambda");
        //foreach() lambda
        list1.forEach(x-> System.out.println(x));

        //函数调用
        list1.forEach(System.out::println);

        //Consumer:消费型函数式接口
        //Stream流
        System.out.println("stream流");
//        filter：断言表达式 条件判断
        list1.stream().filter(x->x.length()>=3).filter(x->x.startsWith("张")).forEach(System.out::println);
//        count:求个数
        long a=list1.stream().count();
        System.out.println(a);
//        distinct:去重
        System.out.println("-----");
        list1.stream().distinct().forEach(System.out::println);
//        limit:可以将流限制为指定的元素数。
        System.out.println("-----");
        list1.stream().limit(2).forEach(System.out::println);
//        skip()方法可跳过前N个元素。
        System.out.println("-----");
        list1.stream().skip(2).forEach(System.out::println);

        System.out.println("-----");
        //concat(s1,s2):把两个流合并成一个
        Stream<String> stream1= Stream.of("1","2","3","4");
        Stream<String> stream2= Stream.of("11","22","33","44");
        Stream<String> stream=Stream.concat(stream1,stream2);
        stream.forEach(System.out::println);
        System.out.println(stream);


        System.out.println("stream流");


    }
}
